package MyLinkList;

public class MyLinkList implements Ilst {
    
    public class Node {
        public int data;
        public Node prev;
        public Node next;
        
        //构造方法
        public Node(int data) {
            this.data = data;
        }
    }
    
    public Node head;
    public Node last;
    
    @Override//头插法
    public void addFirst(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            last = newNode;
        } else {
            newNode.next = head;
            head.prev = newNode;
            //更新头节点
            head = newNode;
        }
    }
    
    @Override//尾插法
    public void addLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            last = newNode;
        } else {
            newNode.prev = last;
            last.next = newNode;
            //更新尾部节点
            last = newNode;
        }
    }
    
    @Override//任意位置插入
    public void addIndex(int index, int data) {
        //对输入位置进行判断
        if (index < 0 || index > size()) {
            System.out.println("输入位置不合法");
            return;
        }
        if (index == 0) {
            //如果插入位置在最前面就使用头插
            addFirst(data);
            return;
        }
        if (index == size()) {
            //如果插入位置在最后面就使用尾插
            addLast(data);
            return;
        }
        //在中间插入
        Node newNode = new Node(data);
        //找到要插入的位置
        Node cur = head;
        while (index != 1) {
            cur = cur.next;
            index--;
        }
        //将新节点插入到cur之前
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;
//        //将新节点插入到cur之后
//        newNode.next = cur.next;
//        newNode.prev = cur;
//        cur.next = newNode;
//        newNode.next.prev = newNode;
    
    }
    
    @Override//查找是否包含关键字key是否在链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null){
            if (cur.data == key){
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }
    
    @Override//删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node cur = head;
        while (cur != null) {
            if(cur.data == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        //只有一个节点 且是需要删除的节点
                        last = null;
                    }
                }else {
                    if(cur.next != null) {
                        //删除中间节点
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    
    @Override//删除所有值为key的节点
    public void removeAllKey(int key) {
        Node cur = head;
        while (cur != null) {
            if(cur.data == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        //只有一个节点 且是需要删除的节点
                        last = null;
                    }
                }else {
                    if(cur.next != null) {
                        //删除中间节点
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    
    @Override//得到单链表的长度
    public int size() {
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    
    @Override//展示链表
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    
    @Override//清空链表
    public void clear() {
        Node cur = head;
        while (cur != null){
            Node tempNode = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = tempNode;
        }
        this.head = null;
        this.last = null;
    }
    
    
}
